﻿using duqin.Common;
using duqin.Entities;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace duqin.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ArticlesController : ControllerBase
    {
        private readonly ILogger<ArticlesController> _logger;
        private readonly IFreeSql _freeSql;

        public ArticlesController(ILogger<ArticlesController> logger, IFreeSql freeSql)
        {
            _logger = logger;
            _freeSql = freeSql;
        }

        // GET: api/<ArticlesController>
        [HttpGet]
        public async Task<IActionResult> Get(int page = 1, int limit = 10)
        {
            var query = _freeSql.Select<Article>().LeftJoin(a => a.User != null && a.User.Id == a.UserId);
            return ServerResponse.Page(await query.ToPageAsync(page, limit));
        }

        // GET api/<ArticlesController>/5
        [HttpGet("{id}")]
        public async Task<IActionResult> Get(int id)
        {
            var query = _freeSql.Select<Article>(id).LeftJoin(a => a.User != null && a.User.Id == a.UserId);
            return ServerResponse.Yes(await query.ToOneAsync());
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // POST api/<ArticlesController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // PUT api/<ArticlesController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // DELETE api/<ArticlesController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
